//
//  WebNumberViewController.m
//  jizhiyunduan
//
//  Created by 鞠超 on 16/4/27.
//  Copyright © 2016年 JC. All rights reserved.
//

#import "WebNumberViewController.h"

#import "FangwenTableViewCell.h"
#import "WebNumberDetailViewController.h"
#import "Model_WebSite.h"

@interface WebNumberViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;

//保存数据的数组
@property (nonatomic, strong) NSMutableArray * arr_data;

@end

@implementation WebNumberViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void)p_navi
{
    _lblTitle.text = [NSString stringWithFormat:@"站点列表"];
    _lblTitle.font = [UIFont systemFontOfSize:20];
    
    _btnRight.hidden = YES;
    
    [self addLeftButton:@"fanhui@2x.png"];
}

//左返回
- (void)clickLeftButton:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//隐藏tabbar
-(void)viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
    
    // 马上进入刷新状态
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];
    
    //注册
    [self.tableView registerClass:[FangwenTableViewCell class] forCellReuseIdentifier:@"cell_fangwen"];
    
    //刷新
    __unsafe_unretained UITableView *tableView = self.tableView;
    __unsafe_unretained __typeof(self) weakSelf = self;
    
    // 下拉刷新
    tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [weakSelf p_data_webList];
    }];
    
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    tableView.mj_header.automaticallyChangeAlpha = YES;
    
//    // 上拉加载
//    tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
//
//        // 结束刷新
//        [tableView.mj_footer endRefreshing];
//    }];
}

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr_data.count;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Model_WebSite * model = self.arr_data[indexPath.row];
    
    FangwenTableViewCell * cell = [[FangwenTableViewCell alloc] init];
    
    cell.name.text = model.site_name;
    cell.web_name.text = model.domain;
    
    return cell;
}

- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Model_WebSite * model = self.arr_data[indexPath.row];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    WebNumberDetailViewController * webNumberDetailViewController = [[WebNumberDetailViewController alloc] init];
    webNumberDetailViewController.str_sid = model.sid;
    
    [self showViewController:webNumberDetailViewController sender:nil];
}


#pragma mark - 站点服务列表//包含 站点 和 seo 通用
- (void)p_data_webList
{
    // 结束刷新
    [self.tableView.mj_header endRefreshing];
    
    NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];
    
    DataProvider * dataprovider = [[DataProvider alloc] init];
    
    [dataprovider setDelegateObject:self setBackFunctionName:@"seo_service_list:"];
    
    [dataprovider webListWithUid:[userdefault objectForKey:@"uid"] token:[userdefault objectForKey:@"token"]];
}

#pragma mark - 接口部分
- (void)seo_service_list:(id )dict
{
//    NSLog(@"%@",dict);
    
    self.arr_data = nil;
    
    if([dict[@"status"] integerValue] == 1)
    {
        @try
        {
            NSArray * arr_list = dict[@"data"];
            
            for (NSDictionary * dict in arr_list)
            {
                Model_WebSite * model = [[Model_WebSite alloc] init];
                
                [model setValuesForKeysWithDictionary:dict];
                model.sid = dict[@"id"];
                
                [self.arr_data addObject:model];
            }
        }
        @catch (NSException *exception)
        {
            
        }
        @finally
        {
            dispatch_async(dispatch_get_main_queue(), ^{
                //刷新tableView(记住,要更新放在主线程中)
                [self.tableView reloadData];
            });
        }
    }
}


#pragma mark - 懒加载
- (NSMutableArray *)arr_data
{
    if(_arr_data == nil)
    {
        self.arr_data = [NSMutableArray array];
    }
    
    return _arr_data;
}


@end
